//
//  ST_ColoryLabel.swift
//  btvideo
//
//  Created by Jason on 2022/1/15.
//  Copyright © 2022 JarvanZhang. All rights reserved.
//

import UIKit
import SnapKit


/// 渐变text的Label控件（gradient color to text ）  【重点】 不支持SnapKit  和约束，必须算出Frame 再使用
class ST_ColoryLabel: UIView {
    
    init(text:NSAttributedString,frame:CGRect,colors:[UIColor],startPoint:CGPoint = .init(x: 0, y: 0.5),endPoint:CGPoint = .init(x: 1, y: 0.5)){
        
        super.init(frame: frame)
        
        let viewBounds = CGRect(origin: .zero, size: frame.size)
        let gradient = CAGradientLayer()
        gradient.colors = colors.map{ $0.cgColor }
        gradient.startPoint = startPoint
        gradient.endPoint = endPoint
        gradient.frame = viewBounds
        layer.addSublayer(gradient)
        
        
        let label = UILabel(frame: viewBounds)
        label.attributedText = text
        addSubview(label)
        
        self.mask = label
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
